var _wxCanvas = _interopRequireDefault(require("./wx-canvas")), echarts = _interopRequireWildcard(require("./echarts"));

function _interopRequireWildcard(t) {
    if (t && t.__esModule) return t;
    var e = {};
    if (null != t) for (var a in t) if (Object.prototype.hasOwnProperty.call(t, a)) {
        var r = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(t, a) : {};
        r.get || r.set ? Object.defineProperty(e, a, r) : e[a] = t[a];
    }
    return e.default = t, e;
}

function _interopRequireDefault(t) {
    return t && t.__esModule ? t : {
        default: t
    };
}

Component({
    properties: {
        canvasId: {
            type: String,
            value: "ec-canvas"
        },
        ec: {
            type: Object
        }
    },
    data: {
        width: "",
        height: "",
        src: ""
    },
    ready: function() {
        this.data.ec ? this.data.ec.lazyLoad || this.init() : console.warn('组件需绑定 ec 变量，例：<ec-canvas id="mychart-dom-bar" canvas-id="mychart-bar" ec="{{ ec }}"></ec-canvas>');
    },
    methods: {
        init: function(r) {
            var n = this, t = wx.version.version.split(".").map(function(t) {
                return parseInt(t, 10);
            });
            if (1 < t[0] || 1 === t[0] && 9 < t[1] || 1 === t[0] && 9 === t[1] && 91 <= t[2]) {
                var e = this.data.canvasId, a = this.ctx = wx.createCanvasContext(e, this), c = new _wxCanvas.default(a, e, function() {
                    wx.sleep(100).then(function() {
                        return wx.fPromise("canvasToTempFilePath", {
                            canvasId: e,
                            quality: 1
                        }, n);
                    }).then(function(t) {
                        n.setData({
                            src: t.tempFilePath
                        });
                    });
                });
                echarts.setCanvasCreator(function() {
                    return c;
                }), wx.createSelectorQuery().in(this).select(".img").boundingClientRect(function(t) {
                    var e = t.width, a = t.height;
                    n.setData({
                        width: e,
                        height: a
                    }, function() {
                        "function" == typeof r ? n.chart = r(c, e, a) : n.data.ec && "function" == typeof n.data.ec.onInit ? n.chart = n.data.ec.onInit(c, e, a) : n.triggerEvent("init", {
                            canvas: c,
                            width: e,
                            height: a
                        });
                    });
                }).exec();
            } else console.error("微信基础库版本过低，需大于等于 1.9.91。参见：https://github.com/ecomfe/echarts-for-weixin#%E5%BE%AE%E4%BF%A1%E7%89%88%E6%9C%AC%E8%A6%81%E6%B1%82");
        },
        canvasToTempFilePath: function(t) {
            var e = this;
            t.canvasId || (t.canvasId = this.data.canvasId), this.ctx.draw(!0, function() {
                wx.canvasToTempFilePath(t, e);
            });
        },
        touchStart: function(t) {
            if (this.chart && 0 < t.touches.length) {
                var e = t.touches[0];
                this.chart._zr.handler.dispatch("mousedown", {
                    zrX: e.x,
                    zrY: e.y
                }), this.chart._zr.handler.dispatch("mousemove", {
                    zrX: e.x,
                    zrY: e.y
                });
            }
        },
        touchMove: function(t) {
            if (this.chart && 0 < t.touches.length) {
                var e = t.touches[0];
                this.chart._zr.handler.dispatch("mousemove", {
                    zrX: e.x,
                    zrY: e.y
                });
            }
        },
        touchEnd: function(t) {
            if (this.chart) {
                var e = t.changedTouches ? t.changedTouches[0] : {};
                this.chart._zr.handler.dispatch("mouseup", {
                    zrX: e.x,
                    zrY: e.y
                }), this.chart._zr.handler.dispatch("click", {
                    zrX: e.x,
                    zrY: e.y
                });
            }
        }
    }
});